unit f_vises;
interface
//{$DEFINE D_VISUAL}
uses
  {$IFDEF MSWINDOWS}
  Windows,
  {$ENDIF}
   Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls {$IFDEF D_VISUAL},sample{$ENDIF};

const _bmpfile ='back.bmp';
type
  Tpl_vises = class(TForm)
    Image1: TImage;
    Panel1: TPanel;
    l_vis: TComboBox;
    vis_timer: TTimer;
    procedure Draw_Bg;
    procedure FormCreate(Sender: TObject);
    procedure l_visChange(Sender: TObject);
    procedure vis_timerTimer(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  pl_vises: Tpl_vises;
  vis_id, curr_width, curr_height: Longint;
  _sleep: boolean;
implementation

{$IFNDEF FPC}
{$R *.dfm}
{$ELSE}
{$R *.lfm}
{$ENDIF}
uses  Unit1, adaptors, visuals, utils, pl_consts;
procedure Tpl_vises.FormCreate(Sender: TObject);
var
  lst: TstringList;
  i: Cardinal;
begin
  Draw_bg;
  _sleep := false;
 lst := Get_Files(GetCurrentdir + _slash + vis_dir, VIS_EXT)^;
  if (lst.count > 0) then
    for i := 0 to lst.count - 1 do
    if Reg_Visualization(lst.Strings[i]) > 0 then
      l_vis.Items.Append(lst.Strings[i]);
  lst.Free;
end;

procedure Tpl_vises.Draw_Bg;
var
  bk: TbitMap;
begin
  bk := TbitMap.Create;
  bk.Width := Image1.Width;
  bk.Height := Image1.Height;
  bk.Canvas.Rectangle(0, 0, bk.Width, bk.Height);
  bk.SaveToFile(_bmpfile);
  Image1.Picture.LoadFromFile(_bmpfile);
  bk.free;
end;

procedure Tpl_vises.l_visChange(Sender: TObject);
var
  i: longint;
begin
  vis_timer.Enabled := false;
  //sample.VIS_INIT(Image1.Width, Image1.Height, @PlMain.WAVEDATA, @PLMAIN.FFTDATA);
  i := 0;
  while ((i < l_vis.items.Count) and (l_vis.Text <> l_vis.Items[i])) do
    inc(i);
  if (i <> l_vis.items.count) then
  begin
    INIT_VISUALIZATION(i + 1, Image1.Width, Image1.Height, @PlMain.WAVEDATA, @PLMAIN.FFTDATA);
    vis_id := i;
  end;
  vis_timer.Enabled := true;
end;

procedure Tpl_vises.vis_timerTimer(Sender: TObject);
begin
  if ((Image1.Width <> curr_width) and (Image1.Height <> curr_height)) then
  begin
    curr_width := Image1.width;
    curr_height := Image1.Height;
    Draw_Bg;
    //Image1.Picture := _bmpfile;    //sample.VIS_INIT(Image1.Width, Image1.Height, @PlMain.WAVEDATA, @PLMAIN.FFTDATA);
    INIT_VISUALIZATION(vis_id + 1, Image1.Width, Image1.Height, @PlMain.WAVEDATA, @PLMAIN.FFTDATA);
  end;
  visuals.Run_Visualization(Image1.Canvas.Handle);
  Image1.refresh;
end;

procedure Tpl_vises.FormHide(Sender: TObject);
begin
  vis_timer.enabled := false;
  _sleep := true;
end;

procedure Tpl_vises.FormShow(Sender: TObject);
begin
  if _sleep then vis_timer.enabled := true;
end;

end.